package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	未完成
 * http://www.chinabidding.com/zbzx-detail-215158654.html  中国国际招标网
 *
 * @author KK
 *
 */
public class ChinabiddingNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/
		"http://www.chinabidding.com/.*-detail-[0-9]+-.*.html",
		"http://www.chinabidding.com/.*-detail-[0-9]+.html",
		"http://www.chinabidding.com/bidDetail/[0-9]*-[A-Za-z]*.html",
		"http://www.chinabidding.com/[a-z]*/[a-z]*/[A-Za-z]*.htm",
		//"http://www.chinabidding.com/search/proj.htm[?]zoneCode=[0-9]**",
		"http://www.chinabidding.com/bidDetail/[0-9]*.html",
		"http://www.chinabidding.com/[a-z]*",
		"http://www.chinabidding.com/infoDetail/[0-9]*-News.html"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);

		title =doc.select("h2").text();
		if(StringUtils.isBlank(title)){
			title=doc.select(".as-article h3").text();
		}
		content=HtmlCleaner.getContentHtml(url ,doc.select("div.gg-conte"));
		if(StringUtils.isBlank(content)){
			content=doc.select(".as-article-body").html();
		}

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");

		date=DateUtils.matchDate(doc.select("div.gg-xl-fbsj").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select(".release-time").text());
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		news.setType(1);
		return news;
	}
	public static void main(String[] args) {
		String url="http://www.chinabidding.com/infoDetail/229491045-News.html";
		ChinabiddingNewsAnalyse analyse=new ChinabiddingNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
